import os,shutil,function
import mysql.connector
from datetime import datetime

mydb = mysql.connector.connect(
  host="192.168.10.81",
  user="root",
  passwd="",
  database="xiaocheng"
)
# 获取最大id
mycursor = mydb.cursor()
mycursor.execute("select max(id) from data_model") 
myresult = mycursor.fetchone()
begin_id=myresult[0]+1



pic_path1 = 'D:/Desk/系统图库/model/全球童装/'
pic_path2 = 'D:/Desk/系统图库/model/TEMU男装/'
pic_path3 = 'D:/Desk/系统图库/model/TEMU女装/'
pic_path4 = 'D:/Desk/系统图库/model/TEMU童装/'
pic_path5 = 'D:/Desk/系统图库/model/TEMU亚洲女装/'
pic_paths = function.get_file_paths(pic_path1)+function.get_file_paths(pic_path2)+function.get_file_paths(pic_path3)+function.get_file_paths(pic_path4)+function.get_file_paths(pic_path5)

db_paths = []
mycursor.execute("select path from data_model where category = '全球童装' or category = 'TEMU男装' or category = 'TEMU女装' or category = 'TEMU童装' or category = 'TEMU亚洲女装'")
myresult = mycursor.fetchall()
for x in myresult:
    db_paths.append(x[0])


now = datetime.now()
formatted_date = now.strftime("%Y-%m-%d")

datas=[]
i=0
for pic_path in pic_paths:
    file_name=os.path.basename(pic_path)
    pic_path=pic_path.replace('\\','/')
    if pic_path not in db_paths:
        dict=[] 
        pic_path=pic_path.replace('\\','/')
        name='DB' +str(begin_id + 1000 + i)
        pic=name+os.path.splitext(file_name)[1]
        new_path="Y:/pic/model/"+pic
        shutil.copy(pic_path,new_path)
        dict.append(name)
        dict.append(pic)
        dict.append(pic_path)
        dict.append(pic_path.split('/')[4])
        if pic_path.split('/')[4]=='全球童装':
            topic=pic_path.split('/')[5].split('#')[0]
            dict.append(topic)      
            dict.append(pic_path.split('/')[6])        
            dict.append(formatted_date)        
            dict.append("镜像添加")
            style_name=pic_path.split('/')[5].split('#')[1]+'#'+pic_path.split('/')[6]
            dict.append(style_name)
        if pic_path.split('/')[4]=='TEMU男装' or pic_path.split('/')[4]=='TEMU女装' or pic_path.split('/')[4]=='TEMU童装' or pic_path.split('/')[4]=='TEMU亚洲女装':
            topic='默认'
            dict.append(topic)      
            dict.append(pic_path.split('/')[6])        
            dict.append(formatted_date)        
            dict.append("镜像添加")
            style_name=pic_path.split('/')[5]+'#'+pic_path.split('/')[6]
            dict.append(style_name)
            print(style_name)
        datas.append(tuple(dict))
        i+=1
        


print(  datas)

# 插入数据
sql = "insert into data_model (name,pic,path,category,topic,color,date,add_sort,style_name,belong) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,'小成')" 
mycursor.executemany(sql, datas) 
mydb.commit()    # 数据表内容有更新，必须使用到该语句
 
print(mycursor.rowcount, "记录插入成功。")

mycursor.close()
mydb.close()